using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using BitMango;
using GTA;
using NI;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

namespace SevenPieceGame
{
    public class MainManager : MonoSingleton<MainManager>
    {
        //现金兑换
        public UIShop m_UIShop;
        //现金UI入口
        public GameObject m_BtnShopEnter;
        //现金UI入口2 带进度显示的
        public UIShopButton m_UIShopButton;
        //新手引导
        public UIGuide m_UIGuide;
        public UIAmazingMatch m_UIAmazingMatch;
        //跑马灯常驻
        public UIPermanent m_UIPermanent;
        //广告加载错误
        public GameObject m_AdError;

        public Action m_PlayTopUIUpdateAction;
        public Action<int> m_UpdateScore;
        
        
        public bool m_isOpenUI;

        public float m_CurStagePlayTime;

        public int m_CurTempPieceCount;
        public void Init()
        {
            
        }

        void Update()
        {
            TTicker.tUpdate();
        }
        
        public void ShowNotiveStr()
        {
            NI.TTicker.AddCallback((o) =>
            {
                if (UserLayerManager.Instance.IsInstallOrLowUser())
                {
                    if (m_UIPermanent == null)
                    {
                        m_UIPermanent = GameObject.Find("UICanvas").transform.Find("UIPermanent").GetComponent<UIPermanent>();
                    }
                    m_UIPermanent.ShowNotice(GetNotiveStr());
                }
                else
                {
                    m_UIPermanent.gameObject.SetActive(false);
                }
            },10,0);

        }
        
        public void ShowUIAmazingMatch(int scoreFromLine)
        {
            if (m_UIAmazingMatch == null)
            {
                m_UIAmazingMatch = GameObject.Find("UICanvas").transform.Find("UIAmazingMatch").GetComponent<UIAmazingMatch>();
            }

            if (UserLayerManager.Instance.IsInstallOrLowUser())
            {
                m_CurStagePlayTime = 0;
                m_UIAmazingMatch.gameObject.SetActive(true);
                m_UIAmazingMatch.InitData(scoreFromLine);
            }
        }

        public void HideUIAmazingMatch()
        {
            m_RewardCMTime = 0;
            m_UIAmazingMatch.gameObject.SetActive(false);
        }
        
        public void ShowAdError()
        {
            if (m_AdError == null)
            {
                m_AdError = GameObject.Find("UICanvas").transform.Find("AdError").gameObject;
            }
            m_AdError.SetActive(true);
            TTicker.AddCallback(HideAdError,0.5f);
        }

        public void HideAdError(object o)
        {
            m_AdError.SetActive(false);
        }
        
        public void ShowUIGuide()
        {
            if (m_UIGuide == null)
            {
                m_UIGuide = GameObject.Find("UICanvas").transform.Find("UIGuide").GetComponent<UIGuide>();
            }
            m_UIGuide.gameObject.SetActive(true);
            m_UIGuide.ShowGuide();
        }

        public void HideUIGuide()
        {
            m_UIGuide.gameObject.SetActive(false);
        }
        
        public void ShowUIShop()
        {
            if (m_UIShop == null)
            {
                m_UIShop = GameObject.Find("UICanvas").transform.Find("UIShop").GetComponent<UIShop>();
            }
            m_UIShop.gameObject.SetActive(true);   
            m_UIShop.InitData();
            m_isOpenUI = true;
            ADSHelper.HideBannerAd();
        }
        
        public void HideUIShop()
        {
            m_UIShop.gameObject.SetActive(false);
            m_isOpenUI = false;
            CheckShowBtnShop();
            ADSHelper.ShowBannerAd();
        }

        public void CheckShowBtnShop()
        {
            
            if (UserLayerManager.Instance.IsInstallOrLowUser())
            {
                ShowUIShopEnter();
                ShopListItemData _data = UIShopManager.Instance.GetShowItemData();
                if (_data != null)
                {
                    SHowUIShopButton();
                }
                else
                {
                    HideUIShopButton();
                }
            }
            else
            {
                HideUIShopEnter();
                HideUIShopButton();
            }
        }

        public void SHowUIShopButton()
        {
            if (m_UIShopButton == null)
            {
                m_UIShopButton = GameObject.Find("UICanvas").transform.Find("UIShopButton").GetComponent<UIShopButton>();
            }
            m_UIShopButton.gameObject.SetActive(true);
            m_UIShopButton.SetShopBtnData();
        }
        
        public void HideUIShopButton()
        {
            if (m_UIShopButton == null)
            {
                m_UIShopButton = GameObject.Find("UICanvas").transform.Find("UIShopButton").GetComponent<UIShopButton>();
            }
            m_UIShopButton.gameObject.SetActive(false);
        }
        
        public void ShowUIShopEnter()
        {
            /*if (m_BtnShopEnter == null)
            {
                m_BtnShopEnter = GameObject.Find("UICanvas").transform.Find("UIShopEnter").gameObject;
                m_BtnShopEnter.GetComponent<Button>().onClick.AddListener(() =>
                {
                    ShowUIShop();
                });
            }
            m_BtnShopEnter.SetActive(true);*/
        }
        
        public void HideUIShopEnter()
        {
            /*if (m_BtnShopEnter == null)
            {
                m_BtnShopEnter = GameObject.Find("UICanvas").transform.Find("UIShopEnter").gameObject;
            }
            m_BtnShopEnter.gameObject.SetActive(false);*/
        }
        
        //对doller进行操作
        public void AddDoller(int addDoller)
        {
            Wallet.Gain("blockcoin", addDoller, null);
            Wallet.Save();
            if (m_PlayTopUIUpdateAction != null)
            {
                m_PlayTopUIUpdateAction();
            }
            /*int _doller = PlayerPrefsManager.GetInt(PlayerPrefsManager.PLAYER_HISTROY_DOLLER_COUNT);
            _doller += addDoller;
            PlayerPrefsManager.SetInt(PlayerPrefsManager.PLAYER_HISTROY_DOLLER_COUNT, _doller);*/
        }

        public int GetDoller()
        {
            return Wallet.GetItemCount("blockcoin");
            /*int _doller = PlayerPrefsManager.GetInt(PlayerPrefsManager.PLAYER_HISTROY_DOLLER_COUNT);
            return _doller;*/
        }
        
        public void SubDoller(int subDoller)
        {
            Wallet.Use("blockcoin", subDoller, null);
            Wallet.Save();
            /*int _doller = PlayerPrefsManager.GetInt(PlayerPrefsManager.PLAYER_HISTROY_DOLLER_COUNT);
            _doller -= subDoller;
            PlayerPrefsManager.SetInt(PlayerPrefsManager.PLAYER_HISTROY_DOLLER_COUNT, _doller);
            if (m_PlayTopUIUpdateAction != null)
            {
                m_PlayTopUIUpdateAction();
            }*/
        }
        
        public void CheckFisrtDayWatchRewardCM()
        {
            string _saveDay = PlayerPrefsManager.GetString(PlayerPrefsManager.FISRT_DAY_WATCH_REWARD_CM_COUNT_DAY,"");
            if (_saveDay.Equals(""))
            {
                DateTime dt = GTA.Utils.TimeStampToDateUTC();
                string _todayDate = dt.ToString("yyyyMMdd");
                PlayerPrefsManager.SetString(PlayerPrefsManager.FISRT_DAY_WATCH_REWARD_CM_COUNT_DAY,_todayDate);
                AddFisrtDayWatchRewardCMCount();
            }
            else
            {
                DateTime dt = GTA.Utils.TimeStampToDateUTC();
                string _todayDate = dt.ToString("yyyyMMdd");
                if (_saveDay.Equals(_todayDate))
                {
                    //同一天
                    AddFisrtDayWatchRewardCMCount();
                    int _count = GetTodayWatchRewardCMCount();
                    if (_count == 5)
                    {
                        AdjustWrapper.Instance.LogEvent("oxdzb9", null);
                    }
                    else if (_count == 10)
                    {
                        AdjustWrapper.Instance.LogEvent("dbjra5", null);
                    }
                    else if (_count == 15)
                    {
                        AdjustWrapper.Instance.LogEvent("560ax8", null);
                    }
                }
                else
                {
                    //nothing
                }
            }
        }
        
        private void AddFisrtDayWatchRewardCMCount()
        {
            int _count = GetTodayWatchRewardCMCount();
            _count++;
            PlayerPrefsManager.SetInt(PlayerPrefsManager.FISRT_DAY_WATCH_REWARD_CM_COUNT,_count);
        }
        
        private int GetTodayWatchRewardCMCount()
        {
            return PlayerPrefsManager.GetInt(PlayerPrefsManager.FISRT_DAY_WATCH_REWARD_CM_COUNT, 0);
        }
        
        public void CheckThreeDayWatchInterCM()
        {
            int _dayCount = PlayerPrefsManager.GetInt(PlayerPrefsManager.THREE_DAY_WATCH_INTER_CM_COUNT_DAY_COUNT, 1);
            if (_dayCount <= 3)
            {
                string _saveDay = PlayerPrefsManager.GetString(PlayerPrefsManager.THREE_DAY_WATCH_INTER_CM_COUNT_LAST_DAY,"");
                if (_saveDay.Equals(""))
                {
                    DateTime dt = GTA.Utils.TimeStampToDateUTC();
                    string _todayDate = dt.ToString("yyyyMMdd");
                    PlayerPrefsManager.SetString(PlayerPrefsManager.THREE_DAY_WATCH_INTER_CM_COUNT_LAST_DAY,_todayDate);
                    AddThreeDayWatchInterCMCount();
                }
                else
                {
                    DateTime dt = GTA.Utils.TimeStampToDateUTC();
                    string _todayDate = dt.ToString("yyyyMMdd");
                    if (_saveDay.Equals(_todayDate))
                    {
                        //同一天
                        AddFisrtDayWatchRewardCMCount();
                        int _count = GetThreeWatchInterCMCount();
                        if (_count == 15)
                        {
                            AdjustWrapper.Instance.LogEvent("y1cx80", null);
                        }
                        else if (_count == 25)
                        {
                            AdjustWrapper.Instance.LogEvent("k9c7j9", null);
                        }
                        else if (_count == 35)
                        {
                            AdjustWrapper.Instance.LogEvent("uob98z", null);
                        }
                    }
                    else
                    {
                        _dayCount++;
                        if (_dayCount <= 3)
                        {
                            PlayerPrefsManager.SetString(PlayerPrefsManager.THREE_DAY_WATCH_INTER_CM_COUNT_LAST_DAY,_todayDate);
                            PlayerPrefsManager.SetInt(PlayerPrefsManager.THREE_DAY_WATCH_INTER_CM_COUNT_DAY_COUNT, _dayCount);
                            AddThreeDayWatchInterCMCount();
                            int _count = GetThreeWatchInterCMCount();
                            if (_count == 15)
                            {
                                AdjustWrapper.Instance.LogEvent("y1cx80", null);
                            }
                            else if (_count == 25)
                            {
                                AdjustWrapper.Instance.LogEvent("k9c7j9", null);
                            }
                            else if (_count == 35)
                            {
                                AdjustWrapper.Instance.LogEvent("uob98z", null);
                            }
                        }
                    }
                }
            }
        }
        
        private void AddThreeDayWatchInterCMCount()
        {
            int _count = GetTodayWatchRewardCMCount();
            _count++;
            PlayerPrefsManager.SetInt(PlayerPrefsManager.THREE_DAY_WATCH_INTER_CM_COUNT,_count);
        }
        
        private int GetThreeWatchInterCMCount()
        {
            return PlayerPrefsManager.GetInt(PlayerPrefsManager.THREE_DAY_WATCH_INTER_CM_COUNT, 0);
        }
        public float m_LastShowInterstitialAdTime;
        public float m_RewardCMTime;
        public float m_RewardCMTimeMax = 240f;

        public void InitIapMgr()
        {
            //初始化iapmgr
            List<string> iapKeys = new List<string>();
            for (int i = 1; i <= 8; i++)
            {
                iapKeys.Add(i.ToString());
            }

            IAPMgr.Instance.Init(iapKeys);
        }
        
        public string GetNotiveStr()
        {
            //UID:243***433  cashed out $1k by cash at 2024-02-28 10:32:04
            StringBuilder _noticeStr = new StringBuilder();
            _noticeStr.Append("UID:<color=green>");
            _noticeStr.Append(Random.Range(100,999));
            _noticeStr.Append("***");
            _noticeStr.Append(Random.Range(100, 999));
            _noticeStr.Append("</color> cashed out <color=yellow>$");
            _noticeStr.Append(GetNoticeNum());
            _noticeStr.Append("k</color> by cash at <color=orange>");
            _noticeStr.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            _noticeStr.Append("</color>");
            return _noticeStr.ToString();
        }
        
        private Dictionary<int, int> weightedNumbers;
        
        private int GetNoticeNum()
        {
            if (weightedNumbers == null)
            {
                weightedNumbers = new Dictionary<int, int>
                {
                    {1, 50},
                    {2, 20},
                    {3, 15},
                    {4, 10},
                    {5, 5}
                };
            }

            int _weight = 0;

            foreach (var kvp in weightedNumbers)
            {
                _weight += kvp.Value;
            }
        
            int randomNumber = Random.Range(1, _weight); // 生成1到100之间的随机数

            if (randomNumber <= 50)
            {
                return 1; // 权重为50，返回数字1
            }
            else if (randomNumber > 50 && randomNumber <= 70)
            {
                return 2; // 权重为20，返回数字2
            }
            else if (randomNumber > 70 && randomNumber <= 85)
            {
                return 3; // 权重为15，返回数字3
            }
            else if (randomNumber > 85 && randomNumber <= 95)
            {
                return 4; // 权重为10，返回数字4
            }
            else
            {
                return 5; // 权重为5，返回数字5
            }
        }
    }
}


